﻿/*
* 简历中心--我的面试
* 2016-7-14
* */
$(function(){
    //面试安排页面,删除左侧日期重复的面试安排标题
    var modifyDateStr = '', planDateStr = '';
    $('.left_txt').each(function(){//需要我修改时间的面试邀请
        var t = $(this), date = t.attr('date');
        if(modifyDateStr == date){
            t.css({'display': 'none'});
        }else{
            modifyDateStr = date;
        }
    });
    $('.left_txtspec').each(function(){//今天及以后的面试安排
        var t = $(this), date = t.attr('date');
        if(planDateStr == date){
            t.css({'display': 'none'});
        }else{
            planDateStr = date;
        }
    });
    //按钮：(我的面试安排包括到面)不去了，告诉一声HR(notInterview)
    //按钮：(待确认的意向)橄榄枝---拒绝(notConsider)、到面---暂不考虑(notConsider)、
    //按钮：(企业反馈以及消息中心企业反馈弹窗)到面--暂不考虑(到面btn_no_interview)、到面--不去了，告诉一声HR(btn_no_usually)、普通投递职位---不去了，告诉一声HR(user_btn_no)
    //按钮：(职位邀请即橄榄枝)橄榄枝---残忍拒绝(newbuttonwhite)
    $(document).on('click', '.notInterview,.notConsider,.user_btn_no,.btn_no_interview,.btn_no_usually,.newbuttonwhite', function(){
        var t = $(this), layerIndex, wrapId = 'refusalReasons';
        //标签中自定义属性data-sourcetype的值为'1'表示面职位、'3'或'11'为普通职位、'0'橄榄枝
        var msgType = t.data('sourcetype');
        //防止按钮被连续点击多次
        if(t.css('cursor') == 'not-allowed'){
            return;
        }else{
            t.css('cursor', 'not-allowed');
        }
        layerIndex= refusalReasons({contentBoxId: wrapId, callSubmit: function(data){
            t.css('cursor', 'pointer');
            var url, params, requestType, reason, fRes = data.fillReason || '', resAry = data.reasonAry || [];
            resAry.push(fRes);
            reason = '[';
            $.each(resAry, function(i, v){
                reason += '"' + encodeURIComponent(v) + '"' + ',';
            });
            reason = reason.replace(/,$/, '');
            reason += ']';
            if(msgType == '0'){//橄榄枝拒绝的url
                requestType = 'post';
                //不管是否成功都返回{"result":"1"}
                url = 'http://i.zhaopin.com/PositionInvited/jobInvited/SaveFeedback';
                //由于在不同的页面中橄榄枝用于拒绝邀请的按钮保存参数的自定义属性不同，为了获取正确的值必须要进行区分
                if(t.hasClass('newbuttonwhite')){//橄榄枝页面中的'残忍拒绝'按钮
                    params = {resume_id: t.data('resumeid'), jobnum: t.data('jobnum'), position_id: t.data('positionid'), company_id: t.data('companyid'), root_company_id: t.data('rootcompanyid'), arrContent: reason};
                }else{//我的面试--待确认的意向、企业反馈--面试邀请页签中橄榄枝的'拒绝'按钮
                    params = {resume_id: t.data('resumeid'), jobnum: t.data('jobnumber'), position_id: t.data('jobid'), company_id: t.data('orgid'), root_company_id: t.data('rootcompanyid'), arrContent: reason};
                }
            }else if(msgType == '1'){//到面暂不考虑、不去了，告诉一声HR的url
                requestType = 'post';
                //返回值{"data":true,"IsSuccess":true,"message":"成功","code":200,"taskId":"5146eb1f-330f-4094-b268-64c662578ec5","time":"2016-07-28 14:14:30"}
                url = 'http://i.zhaopin.com/FacetoFaceInterview/FaceToFace/UserPassInterView';
                params = {resumeid: t.data('resumeid'), jobNumber: t.data('jobnumber'), jobId: t.data('jobid'), orgid: t.data('orgid'), interviewid: t.data('interviewid'), type: t.data('type'), reasons: reason};
                if(t.hasClass('notConsider') || t.hasClass('btn_no_interview')){
                    params.channel = 0;//表示'暂不考虑'
                }else if(t.hasClass('notInterview') || t.hasClass('btn_no_usually')){
                    params.channel = 1;//表示'不去了，告诉一声HR'
                }
            }else if(msgType == '3' || msgType == '11'){//普通反馈通职位'不去了，告诉一声'的url
                requestType = 'post';
                //返回值{"flag":true,"modified":"2016-07-27 09:44"}
                url = 'http://i.zhaopin.com/PositionRecord/jobpostrecord/UpdateMsgStatus';
                //t.data('type')的为6表示拒绝
                params = {type: t.data('type'), companyId: t.data('companyid'), rootid: t.data('rootid'), id: t.data('id'), relateId: t.data('relateid'), gids: t.data('gids'),
                    jtitle: encodeURIComponent(t.data('jtitle')), jobNum: t.data('jobnum'), resumenum: t.data('resumenum'), arrContent: reason};
                if(t.data('isdownload')){
                    params.isdownload = t.data('isdownload');
                }
            }else{
                return;
            }
            params.stamp = new Date().getTime();//添加时间戳
            //防止按钮被连续点击多次
            var submitB = data.btnSubmit;
            if(submitB.css('cursor') == 'not-allowed'){
                return;
            }else{
                submitB.css('cursor', 'not-allowed');
            }
            $.ajax({
                url : url,
                type : requestType,
                data : params,
                success : function(data){
                    submitB.css('cursor', 'pointer');
                    //请求成功
                    if(data.IsSuccess == true || data.flag == true || msgType == '0'){
                        layer.close(layerIndex);//关闭弹窗

                        if(t.hasClass('notInterview')){
                            //(我的面试安排中的到面和橄榄枝)'不去了，告诉一声HR'按钮 请求成功后的操作
                            var btnDiv = t.closest('.viewbot');
                            //删除按钮,改变消息的样式
                            btnDiv.addClass('porela').html('<span>已拒绝</span>');
                            btnDiv.siblings('.viewtop').find('div a').addClass('colb5b');
                            var perMsgDiv = btnDiv.siblings('.viewmid');
                            perMsgDiv.find('.common_blue span').addClass('cola8d');
                            perMsgDiv.find('.common_black span').addClass('colb5b');
                            perMsgDiv.find('.view_address span:eq(1)').addClass('colb5b');
                        }else if(t.hasClass('notConsider')){
                            //(待确认的意向)橄榄枝---拒绝、(待确认的意向)到面---暂不考虑 按钮请求成功后的操作
                            //t.closest('.confirm_list').remove();//删除整掉消息
                            window.location.replace(window.location.href);//刷新当前页面
                        }else if(t.hasClass('btn_no_interview') || t.hasClass('btn_no_usually') || t.hasClass('user_btn_no')){
                            //(企业反馈以及消息中心企业反馈弹窗)到面--暂不考虑(btn_no_interview)、到面--不去了，告诉一声HR(btn_no_usually)、普通投递职位---不去了，告诉一声HR(user_btn_no)
                            changeInterviewStatus(data, t);
                        }else if(t.hasClass('newbuttonwhite')){
                            //(橄榄枝即职位邀请页面)拒绝(newbuttonwhite)
                            if ($.cookie('isshow')) {//记录cookie，如果有cookie值就不会再次弹出弹窗
                                $.cookie('isshow', null, {domain:'i.zhaopin.com', path:'/'});
                                FEEDBACK.dialog && FEEDBACK.dialog.onclose();
                            }
                            FEEDBACK.setTab($('#setTab li').eq(2));//跳转到拒绝标签
                        }
                    }else{
                        zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求返回结果不正确</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                    }
                },
                error : function(data){
                    submitB.css('cursor', 'pointer');
                    zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求出错</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                }
            });
            switch(data.reasonHave){
                case 'yes': {//按钮：提交评价
                    dyweTrackEvent('luDaomian','clickCommit');
                    break;
                }
                case 'no': {//按钮：不选原因，直接拒绝
                    dyweTrackEvent('luDaomian','clickCommit2');
                    break;
                }
            }
        }, callClose: function(){
            t.css('cursor', 'pointer');
        }});
    });
    //按钮：(职位邀请即橄榄枝)---欣然应聘(newbuttongreen)
    //按钮：(我的面试--待确认意向)橄榄枝---接受(acceptInvitation)、普通反馈通---接受邀请(acceptInvitation)、到面---接受邀请(acceptInvitation 点按钮后会跳转)
    //按钮：(企业反馈)普通反馈通职位---接受邀请(user_btn_yes)、到面---修改面试时间,接受邀请(btn_yes_interview 点按钮后会跳转)
    $(document).on('click', '.acceptInvitation,.newbuttongreen,.user_btn_yes', function(){
        var t = $(this), layerIndex, wrapId = 'accept';
        //标签中自定义属性data-sourcetype的值为'1'表示面职位、'3'或'11'为普通职位、'0'橄榄枝
        var url, params, requestType, msgType = t.data('sourcetype');
        //防止按钮被连续点击多次
        if(t.css('cursor') == 'not-allowed'){
            return;
        }else{
            t.css('cursor', 'not-allowed');
        }
        if(msgType == '0'){//橄榄枝接受邀请的url
            requestType = 'post';
            //不管是否成功都返回{"result":"1"}
            url = 'http://i.zhaopin.com/PositionInvited/jobInvited/UpdateInvitedStatus';
            //由于在不同的页面中橄榄枝用于接受邀请的按钮保存参数的自定义属性不同，为了获取正确的值必须要进行区分
            if(t.hasClass('newbuttongreen')){//橄榄枝页面中的'欣然应聘'按钮
                params = {resumeId: t.data('resumeid'), jobnum: t.data('jobnum'), positionId: t.data('positionid'), companyId: t.data('companyid'), rootCompanyId: t.data('rootcompanyid')};
            }else{//我的面试--待确认意向页面中橄榄枝的'接受'按钮
                params = {resumeId: t.data('resumeid'), jobnum: t.data('jobnumber'), positionId: t.data('jobid'), companyId: t.data('orgid'), rootCompanyId: t.data('rootcompanyid')};
            }
        }else if(msgType == '3' || msgType == '11'){//普通反馈通职位'接受邀请'的url
            requestType = 'post';
            //返回值{"flag":true,"modified":"2016-07-27 09:44"}
            url = 'http://i.zhaopin.com/PositionRecord/jobpostrecord/UpdateMsgStatus';
            //t.data('type')的为5表示接受
            params = {type: t.data('type'), companyId: t.data('companyid'), rootid: t.data('rootid'), id: t.data('id'), relateId: t.data('relateid'), gids: t.data('gids'),
                jtitle: encodeURIComponent(t.data('jtitle')), jobNum: t.data('jobnum'), resumenum: t.data('resumenum')};
            if(t.data('isdownload')){
                params.isdownload = t.data('isdownload');
            }
        }else{
            return;
        }
        params.stamp = new Date().getTime();//添加时间戳
        //接受邀请
        $.ajax({
            url : url,
            type : requestType,
            data : params,
            success : function(data){
                t.css('cursor', 'pointer');
                if(data.IsSuccess == true || data.flag == true || msgType == '0'){//提交成功
                    layerIndex = acceptConfirmTips({contentBoxId: wrapId, callOk: function(){
                        layer.close(layerIndex);
                        //(待确认的意向页面)橄榄枝---接受、普通反馈通---接受邀请 按钮请求成功后在点击提心弹窗的我知道了按钮后刷新当前页面
                        if(t.hasClass('acceptInvitation')){
                            window.location.replace(window.location.href);//刷新当前页面
                        }
                    }});
                    if(t.hasClass('newbuttongreen')){//(橄榄枝即职位邀请)---欣然应聘 按钮请求成功后的操作
                        FEEDBACK.getSubmitResponse(true, data, t);
                    }else if(t.hasClass('user_btn_yes')){//(企业反馈)普通反馈通职位---接受邀请 按钮请求成功后的操作
                        changeInterviewStatus(data, t);
                    }
                }else{
                    zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求返回结果不正确</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                }
            },
            error : function(data){
                t.css('cursor', 'pointer');
                zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求出错</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
            }
        });
    });
    //按钮：(过往面试邀请的到面和橄榄枝)---我已到场面试alreadyInterview、评价面试evaluateInterview
    //(企业反馈和消息中心)我已到场面试和评价面试remind_btn_interview
    $(document).on('click', '.alreadyInterview,.evaluateInterview,.remind_btn_interview', function(){
        //标签中自定义属性data-sourcetype的值为'1'表示面职位、'3'或'11'或'12'为普通职位、'0'橄榄枝
        var t = $(this), cName = '', oi = t.data('orgid'), gi = t.data('gid'), ji = t.data('jobid'), ri = t.data('resumeid'),
            ty = t.data('type'), ii = t.data('interviewid'), jn = t.data('jonnumber'), is = t.data('interviewstatus');
        //如果是企业反馈的选择面试时间则结束执行
        if(t.hasClass('remind_btn_interview') && t.text() == '选择面试时间'){
            return;
        }
        if(t.hasClass('alreadyInterview') || t.hasClass('evaluateInterview')){
            cName = t.closest('.view_list').find('.companyName a').html()
        }else{
            cName = t.closest('.fb_list_interview').siblings('#recordid_' + t.data('id')).find('h2 a').attr('title') || t.data('jobname');
        }
        //防止按钮被连续点击多次
        if(t.css('cursor') == 'not-allowed'){
            return;
        }else{
            t.css('cursor', 'not-allowed');
        }
        //获取评价信息
        $.ajax({
            url : 'http://i.zhaopin.com/FacetoFaceInterview/FaceToFace/GetInterViewComment',//只要发出请求后台就会把我已到场面试的改为评价面试
            type : 'get',
            data : {orgid: oi, gid: gi, jobid: ji, resumeid: ri, type: ty,interviewId: ii, jonNumber: jn, interviewstatus: is, stamp: new Date().getTime()},
            success : function(data){
                var wrapId = 'evaluateBox', layerIndex;
                t.css('cursor', 'pointer');
                //获取数据成功
                if(data.IsSuccess){
                    if(data.IsNewComment == true){//未评价显示评价信息提交窗口
                        layerIndex = evaluateInterview({name: cName, contentBoxId: wrapId, callSubmit:function(data){
                            var submitB = data.btnSubmit;
                            //防止按钮被连续点击多次
                            if(submitB.css('cursor') == 'not-allowed'){
                                return;
                            }else{
                                submitB.css('cursor', 'not-allowed');
                            }
                            //提交评价
                            $.ajax({
                                url : 'http://i.zhaopin.com/FacetoFaceInterview/FaceToFace/SetInterViewComment',
                                type : 'post',
                                data : {orgid: oi, gid: gi, jobid: ji, resumeid: ri, type: ty,interviewId: ii, content: data.contStr, interviewstatus: is, bad: data.badStr, good: data.goodStr, stamp: new Date().getTime()},
                                success : function(data){
                                    submitB.css('cursor', 'pointer');
                                    if(data.IsSuccess == true){//提交成功
                                        //把按钮的文本'我已到场面试'改成'评价面试'
                                        t.html('评价面试');
                                        layer.close(layerIndex);
                                    }else{
                                        zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">评价不成功</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                                    }
                                },
                                error : function(data){
                                    submitB.css('cursor', 'pointer');
                                    zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求出错</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                                }
                            });
                            dyweTrackEvent('luDaomian','commitDiscuss');
                        }, callClose: function(){
                            //因为只要发出请求后台就会把我已到场面试的改为评价面试，所以不管用户是否提交评价，关闭评价弹窗后都要把按钮的文本我已到场面试的改为评价面试
                            t.html('评价面试');
                        }});
                    }else if(data.IsNewComment == false){//已评价显示评价信息展示窗口
                        if(!data.Comment){
                            zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">返回数据不正确' + data.Comment + '</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                            return;
                        }
                        layerIndex = evaluationInfo({name: cName, contentBoxId: wrapId, comment: data.Comment});
                    }
                }else{
                    zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求返回结果不正确</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
                }
            },
            error : function(data){
                t.css('cursor', 'pointer');
                zpLayer.showTipCommon({title:"温馨提示",btn: ['<span class="common_btn">确定</span>'],shift:0,content:'<p class="pageTips">请求出错</p>',type:1,shade: [0.5, '#000'],area:['450px','260px']});
            }
        });
    });
    //企业反馈页面的反馈通或到面或消息中心反馈弹窗拒绝时对所在页面面试状态的修改
    function changeInterviewStatus(data, $ele){
        var time, tip, btnType, cls, recordType = $ele.data('sourcetype'), $status_record = $('span[data-recordid=' + $ele.data("id") +']'), $status_html = $status_record.html() || '';
        if(recordType == '1'){//到面
            time = data.time.replace(/:\d\d$/, '');
            tip = '拒绝';
            btnType = 6;
            cls = 'cross';
            //删除公司相关的信息
            var companyInfo = $('.msg_wrap:not(".ash")');
            if($ele.hasClass('btn_no_usually')){//(企业反馈以及消息中心企业反馈弹窗)到面--不去了，告诉一声HR(btn_no_usually) 按钮点击后删除公司信息
                companyInfo.prev('p').remove();
                companyInfo.find('.time_msg01').addClass('time_msg01_other');
                companyInfo.find('.time_msg02').remove();
            }
        }else if(recordType == '3' || recordType == '11'){//普通反馈通
            time = data.modified;
            /*调节ie7地点内容位置兼容*/
            var msgWrap = $(".msg_wrap div");
            msgWrap.eq(2).height(msgWrap.eq(2).find(".path").height());
            msgWrap.eq(5).height(msgWrap.eq(5).find("small").height());
            //5表示接受 6表示拒绝
            btnType = $ele.data('type');
            if(btnType == 5){
                tip = '接受';
                cls = 'bright';
            }else if(btnType == 6){
                tip = '拒绝';
                cls = 'cross';
            }
        }
        //下载简历的反馈弹窗只在消息中心有，弹窗中拒绝和接受按钮的属性isdownload的值为11来表明是下载简历
        //并且弹窗中没有当前所处的面试状态已面试过程
        if($ele.data('isdownload') != 11){
            /*改变该record中按钮“邀请面试”的状态为接受邀请或拒绝邀请*/
            $status_record.data('type', btnType);
            $status_record.html($status_html.replace(/邀请面试|接受邀请/, tip + '邀请'));
            $status_record.data('msgstatus', btnType);
            //更新修改时间
            $status_record.data('time', time);
            $status_record.siblings('.fedback_time').html( );
            /*修改投递状态*/
            var refuseStr = '<div class="fb_time_line_cole ' + cls + '"><i></i><span>' + tip + '邀请</span><small></small></div>';
            if($ele.hasClass('btn_no_usually')){//(企业反馈以及消息中心企业反馈弹窗)到面--不去了，告诉一声HR(btn_no_usually) 按钮点击后把'接受邀请'替换成'拒绝邀请'
                $(".fb_time_line > div").eq(3).html(refuseStr);
            }else{
                $(".fb_time_line > div:last").after(refuseStr);
            }
        }
        /*把以前的信息设置成灰色，并添加新的信息*/
        $('.fb_time_msg .fb_time_msg_con p').eq(0).removeClass('firstP');
        $('.fb_time_msg .fb_time_msg_con .msg_wrap').addClass('ash');
        $('.fb_time_msg .fb_time_msg_con').prepend('<p class="firstP"><span>' + time + '</span><em>你已' + tip + '企业的面试邀请</em></p><div class="btn_wrap">' +
            '<a class="more_btn"href="http://i.zhaopin.com/Recommend/GotoSimilarPosition/GetPositionPara?jobExtId='+ $ele.data('jobnum') +'&r=a">更多靠谱职位</a></div>');
        /*删除拒绝或者不去了按钮*/
        $('.user_wrap').remove();
        /*重新计算左侧边线的长度*/
        var lineHeight = $(".fb_time_msg_con").height()-31;
        $(".fb_time_msg_line").height(lineHeight);
    }
});